<template>
  <div>
  <div v-if="isLogin">
  <el-menu :default-active="activeIndex" class="el-menu-demo" mode="horizontal" @select="handleSelect" >
    <el-menu-item index="1"  v-if="false">职工管理</el-menu-item>

    <el-submenu index="2">
      <template slot="title">宾馆房间</template>
        <el-menu-item index="2-1-4" @click="roomShow">查看房间</el-menu-item>
    </el-submenu>
    <el-submenu index="3">
      <template slot="title">顾客管理</template>
      <el-menu-item index="3-1" @click="regCus()">注册顾客信息</el-menu-item>
      <el-menu-item index="3-2" @click="getCus()">查询顾客信息</el-menu-item>
      <el-menu-item index="3-3" @click="updateCus()">更新顾客信息</el-menu-item>
    </el-submenu>

    <el-submenu index="4">
      <template slot="title">操作</template>
      <el-menu-item index="4-1" @click="createOrder">开房</el-menu-item>
      <el-menu-item index="4-2" @click="finOrder">退房</el-menu-item>
      <el-menu-item index="4-3" @click="updateCus()">办理会员</el-menu-item>
    </el-submenu>
    <el-submenu index="6">
      <template slot="title">订单管理</template>
      <el-menu-item index="4-3" @click="showOrder">查看订单</el-menu-item>
      <el-menu-item index="4-3" @click="getByCon">按条件查询</el-menu-item>
    </el-submenu>

    <el-menu-item index="5" @click="logout()">退出登录</el-menu-item>
    <el-menu-item index="6" @click="backmanger()">管理员页面</el-menu-item>
  </el-menu>
    <div v-if="isNoKey" style="text-align: center">
      <el-form :label-position="labelPosition" label-width="100px" style="text-align: center">
        <el-form-item label="员工编号" >
          <el-input v-model="ems.eid" ></el-input>
        </el-form-item>
        <el-form-item label="员工姓名">
          <el-input v-model="ems.name"></el-input>
        </el-form-item>
        <el-form-item label="账号密码">
          <el-input v-model="ems.passwd"></el-input>
        </el-form-item>
        <el-button type="primary" @click="forceLogout">退出</el-button>
      </el-form>
    </div>
  </div>
    <div style="text-align: center" v-if="!isLogin" >
      <br>
      <br>
      <br>
      <br>
      <br>
      <el-button type="primary" @click="toEmsLogin">点击登录</el-button>
    </div>
  </div>
</template>


<script>
import instance from "@/untils";

export default {
  data() {
    return {
      activeIndex: '1',
      isLogin:false,
      isNoKey:false,
      ems:{}
    };
  },
  methods: {
    handleSelect(key, keyPath) {
      console.log(key, keyPath);
    },
    roomShow(){
      this.$router.push('/roomShow');
    }
   ,
    //3 顾客管理
    regCus(){
      this.$router.push('/regCus');
    },
    getCus(){
      this.$router.push('getCus')
    },
    updateCus(){
      this.$router.push('updateCus')
    },
    forceLogout(){
      this.isNoKey=true;
      instance.post('/ems/forceLogout',this.ems).then(res=>{
        if(res.data.status==200){
          localStorage.removeItem('key')
          localStorage.removeItem('ems')
          alert("退出成功")
          this.$router.push('/emsLogin')
        }
      })
    },
    // 操作
    createOrder(){
      this.$router.push('/createOrder')
    },
    finOrder(){
      this.$router.push('/finishOrder')
    },
    showOrder(){
      this.$router.push('/showOrder')
    },
    getByCon(){
      this.$router.push('/getByCon')
    },
    // 5 职工管理
    backmanger(){
      let token = localStorage.getItem('key');
      instance.post('ems/isManger/'+token).then(res=>{
      if(res.data==true){
          this.$router.push('/mangerHome')
        }else{
          alert("你还不是管理员")
        }
        // alert(res.data)
      })
    },

    logout() {
      let key=localStorage.getItem('key');
      console.log(key);
      if(key!=null){
        instance.get('/ems/logout/'+key).then(res=>{
          console.log(res.data);
          if(res.data.status==200){
            localStorage.removeItem('key')
            localStorage.removeItem('ems')
            this.$router.push('/emsLogin')
            alert("退出成功")
          }
        })
      }else{
         this.isNoKey=true
      }
      this.$store.commit('logouted')
    },

    toEmsLogin(){
      this.$router.push('/emsLogin')
    }
  },
  components:{
  },
  created() {
    let token = localStorage.getItem('key');
    instance.post('ems/getEmsByToken/'+token).then(res=>{
      if(res.data.status==200){
        this.isLogin=true
      }
    })
  }
}
</script>